package com.zhouyipeng;

import java.util.*;

public class TaskWithComparator {
    public static void main(String[] args) {

        // 比较器 先比拓客总数，再比有效拓客数
//        TreeSet<Task>set = new TreeSet<>(new Comparator<Task>() {
//            @Override
//            public int compare(Task t1,Task t2){
//                int num1 = t2.getCount() - t1.getCount();
//                int num2 = (num1 == 0)?t2.getValid() - t1.getValid() : num1;
//
//                return (num2 < 0)? -1 : (num2 == 0 ? 0 : 1);
//            }
//        });

        List<Task> list = new ArrayList<>();

        Task t1 = new Task();
        t1.setCount(20);
        t1.setValid(3);

        Task t2 = new Task();
        t2.setCount(20);
        t2.setValid(1);

        Task t3 = new Task();
        t3.setCount(20);
        t3.setValid(2);

        Task t4 = new Task();
        t4.setCount(30);
        t4.setValid(1);

        Task t5 = new Task();
        t5.setCount(20);
        t5.setValid(1);

        Task t6 = new Task();
        t6.setCount(40);
        t6.setValid(1);

        list.add(t1);
        list.add(t2);
        list.add(t3);
        list.add(t4);
        list.add(t5);
        list.add(t6);

        System.out.println("比较前：");
        list.forEach(one -> System.out.println(one));

        Collections.sort(list,new Comparator<Task>(){
            @Override
            public int compare(Task t1,Task t2){
                int num1 = t2.getCount() - t1.getCount();
                int num2 = (num1 == 0)?t2.getValid() - t1.getValid() : num1;

                return (num2 < 0)? -1 : (num2 == 0 ? 0 : 1);
            }

        });

        System.out.println("比较后：");
        list.forEach(one -> System.out.println(one));
    }
}
